package com.ai.wx.controller;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.ai.core.util.Configure;
import com.ai.wx.entity.AgentUser;
import com.ai.wx.pojo.Auth2AccessToken;
import com.ai.wx.pojo.JsSign;
import com.ai.wx.service.AgentService;
import com.ai.wx.service.SystemService;
import com.ai.wx.util.SignUtil;
import com.ai.wx.util.WeixinUtil;

@Controller
public class BaseController {
	@Autowired
	private AgentService agentService;
	
	private boolean isTest = Configure.isTest();
	@Autowired
	private SystemService systemService;
	protected void setJsSign(HttpServletRequest request) {
	    String url =request.getRequestURL().toString();
	    if(request.getQueryString()!=null){
	    	url +="?"+request.getQueryString();
	    }
		String jsapiTicket = systemService.getJsapiTicket();
		JsSign jsSign;
        try {
	        jsSign = SignUtil.getJsSignature(jsapiTicket, url);
			request.setAttribute("JsSign", jsSign);
        } catch (UnsupportedEncodingException e) { 
	        e.printStackTrace();
        }
    }
	
//	protected Auth2AccessToken getuth2AccessToken(HttpServletRequest request) { 
//		if(isTest){
//			request.getSession().setAttribute("openid", "abc");
//			return null;
//		}
//		if(request.getSession().getAttribute("openid")!=null){
//			Auth2AccessToken Auth2AccessToken = new Auth2AccessToken();
//			Auth2AccessToken.setOpenid((String)request.getSession().getAttribute("openid"));
//			return Auth2AccessToken;
//		}
//		String code =request.getParameter("code");
//		Auth2AccessToken auth2AccessToken= WeixinUtil.getOath2AccessToken(Configure.getAppId(), Configure.getAppsecret(), code);
//		request.getSession().setAttribute("openid", auth2AccessToken.getOpenid());
//		return auth2AccessToken;
//	}
//	
	protected String getOpenId(HttpServletRequest request) { 
		if(isTest){
			return Configure.getTestOpenId();
		}
  		Object obj = request.getSession().getAttribute("openid");
  		if(obj !=null){
  			return (String)obj;
  		}else{
  			String code =request.getParameter("code");
  			Auth2AccessToken auth2AccessToken= WeixinUtil.getOath2AccessToken(Configure.getAppId(), Configure.getAppsecret(), code);
  			request.getSession().setAttribute("openid", auth2AccessToken.getOpenid());
  			return auth2AccessToken.getOpenid();
  		} 
	}
	protected AgentUser getAgentUser(HttpServletRequest request) { 
		 
  			AgentUser agentUser=agentService.getAgentUserByOpenId(getOpenId(request));
  			if(agentUser!=null){
  				request.getSession().setAttribute("agentUser",agentUser);
  				return agentUser;
  			}else{
  				return null;
  			}
	}
	protected AgentUser getAgentUserFromSession(HttpServletRequest request) { 
		Object obj = request.getSession().getAttribute("agentUser");
  		if(obj !=null){
  			return (AgentUser)obj;
  		}else{ 
  			AgentUser agentUser=agentService.getAgentUserByOpenId(getOpenId(request));
  			if(agentUser!=null){
  				request.getSession().setAttribute("agentUser",agentUser);
  				return agentUser;
  			}else{
  				return null;
  			}
  		} 
	}
	
}
